<?php

/**
 * A regisztráció általűnos leírója.
 *
 * @see ActiveRegistration
 * @see PassiveRegistration
 *
 * The followings are the available columns in table 'registration':
 * @property integer $id
 * @property string $type
 * @property string $firstname
 * @property string $lastname
 * @property string $email
 * @property string $phone
 * @property string $birthDate
 * @property string $birthPlace
 * @property string $studies
 * @property string $musics
 * @property string $results
 * @property string $created
 * @property string $modified
 * @property integer $valid
 * @property string activationCode
 */
class Registration extends CActiveRecord
{

   /**
    *
    * @var passzív típus
    */
   const PASSIVE = 'passive';
   /**
    *
    * @var aktív típus
    */
   const ACTIVE = 'active';

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Registration the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'registration';
	}

	/**
	 * We're overriding this method to fill findAll() and similar method result
	 * with proper models.
	 *
	 * @param array $attributes
	 * @return Registration
	 */
	protected function instantiate($attributes){
	    switch($attributes['type']){
	        case self::PASSIVE:
	            $class='PassiveRegistration';
	            break;
	        case self::ACTIVE:
	            $class='ActiveRegistration';
	            break;
	        default:
	            throw new CException("Nem ismert regisztráció típus");
	    }

	    $model=new $class(null);

	    return $model;
	}

	/**
	 * (non-PHPdoc)
	 * @see CModel::beforeValidate()
	 */
	public function beforeValidate(){
	   $this->phone = trim($this->phone, '_');

	   return parent::beforeValidate();
	}

	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::beforeSave()
	 */
	public function beforeSave() {
	    if ($this->isNewRecord)
	        $this->created = new CDbExpression('NOW()');

	    $this->modified = new CDbExpression('NOW()');

	    return parent::beforeSave();
	}


	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{

		return array(
			array('type, firstname, lastname, email, phone, birthDate, birthPlace', 'required', 'message'=> 'Kötelező mező.'),
			array('valid', 'numerical', 'integerOnly'=>true),
		   array('email', 'email', 'message'=> 'Valódi e-mail cím szükséges.'),
		   array('email', 'unique', 'message' => 'Ezzel az e-mail címmel már regisztráltak.'),
			array('type', 'length', 'max'=>10),
		   array('phone', 'match', 'pattern'=> '/^\+\d{2}\d?\s\d{2}(\s\d{3}){2}\d?\s?$/', 'message'=> 'Érvénytelen telefonszám. Érvényes alak: +36 90 123 4567'),
			array('firstname, lastname, email, phone', 'length', 'max'=>255, 'message' => 'Legfeljebb 255 karakter lehet.'),
			array('birthDate, birthPlace, studies, musics, results', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type, firstname, lastname, email, phone, birthDate, birthPlace, studies', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type' => 'Típus',
			'firstname' => 'Keresztnév',
			'lastname' => 'Vezetéknév',
			'email' => 'E-mail',
			'phone' => 'Telefon',
			'birthDate' => 'Születési dátum',
			'birthPlace' => 'Születési hely',
			'studies' => 'Tanulmányok',
			'musics' => 'Válaszott művek',
			'results' => 'Eddigi eredmények',
			'created' => 'Regisztrávla',
			'modified' => 'Módosítva',
			'valid' => 'Érvényes',
		   'recommendation' => 'Tanári ajánlás',
		);
	}

	/**
	 *
	 * @return string fullname
	 */
	public function getFullname(){
	   return $this->lastname . ' ' . $this->firstname;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('birthDate',$this->birthDate,true);
		$criteria->compare('birthPlace',$this->birthPlace,true);
		$criteria->compare('studies',$this->studies,true);
		$criteria->compare('musics',$this->musics,true);
		$criteria->compare('results',$this->results,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
		$criteria->compare('valid',$this->valid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}